package com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator;

import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.ConfigurationTypeListMap;
import com.personal.gateway.utils.StringTemplateUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.ParameterCheckListMap.MAP_SPLIT_KEY;
import static com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.ParameterCheckListString.LIST_SPLIT_KEY;

/**
 * @author BigHead
 * @date 2022/1/11 10:05
 * @description WebObjectGeneratorString
 */
public class WebObjectGeneratorListMap extends AbstractWebObjectGenerator<List<Map<String, String>>, ConfigurationTypeListMap> {

    // 样例数据

    private static final String EXAMPLE_VALUE = "EXAMPLE_VALUE";
    private static final String EXAMPLE_TARGET_ELEMENT = "placeholder=\"示例： ${EXAMPLE_VALUE}\"";

    // 基础输入组件

    private static final String WEB_ID = "WEB_ID";
    private static final String EXAMPLE_TARGET = "EXAMPLE_TARGET";
    private static final String EXIST_VALUES = "EXIST_VALUES";
    private static final String STRING_INPUT_ELEMENT = "<" +
            "input " +
            "type=\"text\" " +
            "id=\"${WEB_ID}\" " +
            "${EXAMPLE_TARGET} " +
            "autocomplete=\"off\" " +
            "class=\"layui-input\" " +
            "${EXIST_VALUES} " +
            ">";

    @Override
    protected String getInputElement(ConfigurationOption<List<Map<String, String>>, ConfigurationTypeListMap> configurationOption) {
        String exampleTarget = "";
        if (null != configurationOption.getConfigurationType().getExampleValue() && configurationOption.getConfigurationType().getExampleValue().length > 0) {
            exampleTarget += StringTemplateUtils.render(
                    EXAMPLE_TARGET_ELEMENT,
                    new HashMap<String, String>(1) {{
                        put(EXAMPLE_VALUE,
                                Arrays.stream(configurationOption.getConfigurationType().getExampleValue())
                                        .map(listMap ->
                                                listMap
                                                        .stream()
                                                        .map(m ->
                                                                m.entrySet()
                                                                        .stream()
                                                                        .map(mapEntry -> mapEntry.getKey() + MAP_SPLIT_KEY + mapEntry.getValue())
                                                                        .collect(Collectors.joining(LIST_SPLIT_KEY))
                                                        )
                                                        .collect(Collectors.joining(LIST_SPLIT_KEY))
                                        ).collect(Collectors.joining(LIST_SPLIT_KEY))
                        );
                    }}
            );
        }
        String finalExampleTarget = exampleTarget;


        return StringTemplateUtils.render(
                STRING_INPUT_ELEMENT,
                new HashMap<String, String>(5) {{
                    put(WEB_ID, webElementIdRecode(configurationOption.getConfigurationKey().getName()));
                    put(EXAMPLE_TARGET, finalExampleTarget);
                    put(ERROR_MARK, null == configurationOption.getConfigurationOptionCheckException() ? "" : ERROR_MARK_CLASS);
                    put(EXIST_VALUES, getExistValue(configurationOption));
                }}
        );
    }

    private String getExistValue(ConfigurationOption<List<Map<String, String>>, ConfigurationTypeListMap> configurationOption) {
        if (null == configurationOption.getConfigurationType().getValue() || configurationOption.getConfigurationType().getValue().isEmpty()) {
            return "";
        } else {
            return "value=\"" +
                    configurationOption.getConfigurationType().getValue()
                            .stream()
                            .map(map ->
                                    map.entrySet()
                                            .stream()
                                            .map(mapEntry -> mapEntry.getKey() + ":" + mapEntry.getValue())
                                            .collect(Collectors.joining(LIST_SPLIT_KEY))
                            )
                            .collect(Collectors.joining(LIST_SPLIT_KEY)) +
                    "\"";
        }
    }
}
